@extends('admin.common.main')
@section('css')
    {{--  引入webuploader css 异步文件上传  --}}
    <link rel="stylesheet" type="text/css" href="/webuploader/webuploader.css">
@endsection
@section('cnt')
    <nav class="breadcrumb">
        <i class="Hui-iconfont">&#xe67f;</i> 首页
        <span class="c-gray en">&gt;</span> 房源管理
        <span class="c-gray en">&gt;</span> 房源修改
        <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px"
           href="javascript:location.replace(location.href);" title="刷新">
            <i class="Hui-iconfont">&#xe68f;</i>
        </a>
    </nav>

    <article class="page-container">

        <form action="{{ route('admin.fang.update',$fang) }}" class="form form-horizontal" method="post"
              id="form-member-add">
            @csrf
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>房源名称：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" name="fang_name" value="{{ $fang->fang_name }}">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>小区名称：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" name="fang_xiaoqu" value="{{ $fang->fang_xiaoqu }}">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>小区地址：
                </label>
                <div class="formControls col-xs-8 col-sm-5">
                    <select name="fang_province" style="width: 100px" class="input-text"
                            onchange="selectcity(this,'fang_city')">
                        <option value="-1">==省==</option>
                        @foreach($data['cityData'] as $item)
                            <option @if($fang->fang_province == $item->id) selected
                                    @endif value="{{ $item->id }}">{{ $item->name }}</option>
                        @endforeach
                    </select>
                    <select name="fang_city" id="fang_city" style="width: 100px" class="input-text"
                            onchange="selectcity(this,'fang_region')">
                        <option value="0">==市==</option>
                        @foreach($cities as $item)
                            <option @if($fang->fang_city == $item->id) selected
                                    @endif value="{{ $item->id }}">{{ $item->name }}</option>
                        @endforeach
                    </select>
                    <select name="fang_region" id="fang_region" style="width: 100px" class="input-text">
                        <option value="0">==区/县==</option>
                        @foreach($regions as $item)
                            <option @if($fang->fang_region == $item->id) selected
                                    @endif value="{{ $item->id }}">{{ $item->name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="formControls col-xs-8 col-sm-4">
                    <input type="text" class="input-text" placeholder="详细地址" name="fang_addr"
                           value="{{ $fang->fang_addr }}">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>租金：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input style="width: 100px" type="text" class="input-text" name="fang_rent"
                           value="{{ $fang->fang_rent }}">元
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>楼层：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input style="width: 100px" type="text" class="input-text" name="fang_floor"
                           value="{{ $fang->fang_floor }}">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>付款方式：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <select name="fang_rent_type" style="width: 100px" class="input-text">
                        @foreach($data['fang_rent_type'] as $item)
                            <option
                                @if($item->id == $fang->fang_rent_type) selected
                                @endif value="{{ $item->id }}">{{ $item->name }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>几室：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input style="width: 100px" value="{{ $fang->fang_shi }}" type="text" class="input-text"
                           name="fang_shi">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>几厅：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input style="width: 100px" value="{{ $fang->fang_ting }}" type="text" class="input-text"
                           name="fang_ting">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>几卫：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input style="width: 100px" value="{{ $fang->fang_wei }}" type="text" class="input-text"
                           name="fang_wei">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>朝向：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <select name="fang_direction" style="width: 100px" class="input-text">
                        @foreach($data['fang_direction'] as $owner)
                            <option
                                @if($owner->id == $fang->fang_direction) selected
                                @endif value="{{ $owner->id }}">{{ $owner->name }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>租赁方式：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <select name="fang_rent_class" style="width: 100px" class="input-text">
                        @foreach($data['fang_rent_class'] as $owner)
                            <option @if($owner->id == $fang->fang_rent_class) selected
                                    @endif value="{{ $owner->id }}">{{ $owner->name }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>建筑面积：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input style="width: 100px" type="text" class="input-text" name="fang_build_area"
                           value="{{ $fang->fang_build_area }}">平方
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>使用面积：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input style="width: 100px" type="text" class="input-text" name="fang_using_area"
                           value="{{ $fang->fang_using_area }}">平方
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>建筑年代：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="date" class='input-text' style="width: 200px" name="fang_year"
                           value="{{ $fang->fang_year }}">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>配套设施：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    @foreach($data['fang_config'] as $item)
                        <input @if(in_array($item->id,$fang->fang_conf)) checked @endif type="checkbox"
                               value="{{ $item->id }}"
                               name="fang_config[]">{{ $item->name }}&emsp;
                    @endforeach
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">房屋图片：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="hidden" class="input-text" id="fang_pic" name="fang_pic" value="{{ $fang->fang_pic }}">
                    <div id="imglist">
                        @foreach($fang->fang_imgg as $pic)
                            <div style="position: relative;;width:100px;">
                                <img src="{{ $pic }}" style="width:160px;height: 100px"/>
                                <strong onclick="delpic(this,'{{ $pic }}')"
                                        style="position: absolute;right: 2px;top: 2px;color: red;font-size: 20px;">X</strong>
                            </div>
                        @endforeach
                    </div>
                    <div id="filePicker">选择图片</div>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>业主：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <select name="fang_owner" style="width: 100px" class="input-text">
                        @foreach($data['ownerData'] as $owner)
                            <option
                                @if($owner->id == $fang->fang_owner) selected
                                @endif value="{{ $owner->id }}">{{ $owner->name }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>是否推荐：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="radio" name="is_recommend" value="0" @if($fang->is_recommend == 0) checked @endif>否&emsp;
                    <input type="radio" name="is_recommend" value="1" @if($fang->is_recommend == 1) checked @endif>是
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>房屋描述：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <textarea name="fang_desn" class="textarea">{{ $fang->fang_desn }}</textarea>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>房源信息：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <textarea name="fang_body" id="body">{{ $fang->fang_body }}</textarea>
                </div>
            </div>

            <div class="row cl">
                <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                    <input class="btn btn-primary radius" type="submit" value="确认修改">
                </div>
            </div>
        </form>
    </article>

    <style>
        .filePicker {
            margin-right: 60px;
            margin-left: 50px;
        }

        .delimg {
            cursor: pointer;
        }
    </style>
@endsection

@section('js')
    {{-- webuploader js 异步文件上传 --}}
    <script type="text/javascript" src="/webuploader/webuploader.js"></script>
    {{--// 引入验证的jquery--}}
    <script type="text/javascript" src="/admin/lib/My97DatePicker/4.8/WdatePicker.js"></script>
    <script type="text/javascript" src="/admin/lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="/admin/lib/laypage/1.2/laypage.js"></script>
    <script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/jquery.validate.js"></script>
    <script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/validate-methods.js"></script>
    <script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/messages_zh.js"></script>

    {{-- webuploader js 异步文件上传 --}}
    <script type="text/javascript" src="/webuploader/webuploader.js"></script>
    <script type="text/javascript" src="/ueditor/ueditor.config.js"></script>
    <!-- 编辑器源码文件 -->
    <script type="text/javascript" src="/ueditor/ueditor.all.js"></script>

    <script>

        // 地区三级联动
        function selectcity(obj, item) {
            // d得到省份ID
            let id = $(obj).val();
            // url地址
            let url = "{{ route('admin.fang.city') }}"
            $.get(url, {id}).then((json) => {
                let html = `<option value="-1">==市==</option>`;
                // 循环
                json.map(item => {
                    let {id, name} = item;
                    html += `<option value="${id}">${name}</option>`
                });
                $("#" + item).html(html);
            });
        }

        var ue = UE.getEditor('body');

        // webupload
        var uploader = WebUploader.create({
            // 选完文件后，是否自动上传。
            auto: true,
            // swf文件路径
            swf: '/js/Uploader.swf',
            // post上传的携带的数据
            formData: {
                _token: "{{ csrf_token() }}"
            },
            // 文件接收服务端。
            server: '{{ route('admin.fang.upimg') }}',
            // 内部根据当前运行是创建，可能是input元素，也可能是flash.
            pick: {
                id: '#filePicker',
                // 是否允许同时上传多个文件
                multiple: false,
            },
            // 只允许选择图片文件。
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/*'
            },
        });
        // 图片上传成功
        uploader.on('uploadSuccess', (file, ret) => {
            // 解决表单提交时，图片以#隔开解决
            let val = $('#fang_pic').val();
            let tmp = val + '#' + ret.src;
            $('#fang_pic').val(tmp);
            // 图片显示
            let imglist = $('#imglist');
            // 注：一定要用追加还是不html覆盖
            let html = `
            <div style="position: relative;;width:100px;">
                <img src="${ret.src}" style="width:160px;height: 100px" />
                <strong onclick="delpic(this,'${ret.src}')" style="position: absolute;right: 2px;top: 2px;color: red;font-size: 20px;">X</strong>
            </div>
        `;
            imglist.append(html);
        });

        // 删除图片
        function delpic(obj, picurl) {
            let url = "{{ route('admin.fang.delimg') }}?file=" + picurl;
            // 发起请求删除
            fetch(url);
            // 删除当前点击图片显示
            $(obj).parent().remove();
            // 修改隐藏域表单
            $('#fang_pic').val($('#fang_pic').val().replace(`#${picurl}`, ''));
        }

        // JS验证提交
        $("#form-member-add").validate({
            rules: {
                fang_name: {
                    required: true
                },
                fang_xiaoqu: {
                    required: true
                },
                fang_province: {
                    min: 1
                },
                fang_city: {
                    min: 1
                },
                fang_region: {
                    min: 1
                },
                fang_addr: {
                    required: true
                },
                fang_year: {
                    required: true
                },
                fang_rent: {
                    number: true
                },
                fang_area: {
                    required: true
                },
                fang_floor: {
                    required: true
                },
                fang_owner: {
                    required: true
                },
                fang_pic: {
                    required: true
                },
                fang_desn: {
                    required: true
                },
                fang_using_area: {
                    required: true
                },
            },
            onkeyup: false,
            success: "valid",
            submitHandler: function (form) {
                let url = $(form).attr('action');
                let data = $(form).serialize();
                $.ajax({
                    url,
                    data,
                    type: "PUT",
                }).then(({msg, status}) => {
                    if (status == 200) {
                        layer.msg(msg, {icon: 1, time: 900});
                        location.href = '{{ route('admin.fang.index') }}';
                    } else {
                        layer.msg(msg, {icon: 2, time: 900});
                    }
                })
            },
        });
    </script>
@endsection
